package com.ry.novel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.mapper.BookCommentMapper;
import com.ry.novel.model.dto.*;
import com.ry.novel.model.dto.condition.CommentQuery;
import com.ry.novel.model.resp.Result;
import com.ry.novel.service.BookCommentService;
import com.ry.novel.service.ReportService;
import com.ry.novel.service.UserBookShelfService;
import com.ry.novel.service.UserService;
import io.swagger.annotations.Api;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ry
 * @since 2022-01-22 14:32
 **/
@Slf4j
@RequestMapping("/user")
@RestController
@Api(tags = "用户接口")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserBookShelfService userBookShelfService;

    @Autowired
    private BookCommentService bookCommentService;

    @Autowired
    private ReportService reportService;

    /**
     * 注册接口
     *
     * @param userInfo
     * @return
     */
    @PostMapping("/register")
    public Result<Map<String, Object>> register(@RequestBody UserInfo userInfo) {
        log.info(userInfo.toString());
        UserInfo info = userService.register(userInfo);
        Map<String, Object> data = new HashMap<>(1);
        data.put("token", jwtTokenUtil.generateToken(info));

        userInfo.setPassword("");
        data.put("userInfo", userInfo);
        return Result.ok(data);
    }


    /**
     * 登录接口
     */
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody UserInfo userInfo) {
        userInfo = userService.login(userInfo);

        Map<String, Object> data = new HashMap<>();
        data.put("token", jwtTokenUtil.generateToken(userInfo));
        data.put("userInfo", userInfo);

        return Result.ok(data);
    }

    /**
     * 测试接口
     *
     * @param request
     * @return
     */
    @GetMapping("/lookUserInfo")
    public Result<UserInfo> lookUserInfo(HttpServletRequest request) {
        UserInfo userInfo = getUserInfo(request);
        UserInfo user = userService.queryUserById(userInfo.getId());

        return Result.ok(user);
    }

    /**
     * 书架接口
     *
     * @return
     */
    @GetMapping("/bookShelf")
    public Result<List<UserBookShelfInfo>> bookShelf() {
        List<UserBookShelfInfo> bookInfos = userBookShelfService.queryCurrentUser();
        return Result.ok(bookInfos);
    }

    @PostMapping("/addBookShelf")
    public Result<Void> addBookShelf(@RequestBody UserBookShelfInfo userBookShelfInfo) {
        userBookShelfService.addBookShelf(userBookShelfInfo);

        return Result.ok();
    }

    @GetMapping("/queryBookInShelf")
    public Result<Boolean> queryBookInShelf(@RequestParam("bookId") Long bookId) {
        Boolean flag = userBookShelfService.queryBookInShelf(bookId);

        return Result.ok(flag);
    }

    @PostMapping("/deleteBookInBookShelf")
    public Result<Void> deleteBookInBookShelf(@RequestParam("id") Long id) {
        userBookShelfService.deleteBookShelf(id);

        return Result.ok();
    }


    @PostMapping("/addReport")
    public Result<Void> addReport(@RequestBody ReportInfo reportInfo) {
        reportService.addReport(reportInfo);

        return Result.ok();
    }

    //发表评论
    @PostMapping("/addComment")
    public Result<Void> addComment(@RequestBody BookCommentInfo bookCommentInfo) {
        bookCommentService.addCommment(bookCommentInfo);

        return Result.ok();
    }

}
